package com.ljx.itbbs.server.article.service.adapter;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.ljx.itbbs.server.article.domain.entity.*;
import com.ljx.itbbs.server.article.domain.enums.ArticleResEnum;
import com.ljx.itbbs.server.article.domain.vo.req.ArticleResourceUploadReq;
import com.ljx.itbbs.server.article.domain.vo.req.ArticleSaveReq;
import com.ljx.itbbs.server.article.domain.vo.req.ArticleUpdateReq;
import com.ljx.itbbs.server.article.domain.vo.req.admin.PlateAdminSaveReq;
import com.ljx.itbbs.server.article.domain.vo.resp.ArticleUpdateInfoResp;
import com.ljx.itbbs.server.article.domain.vo.resp.PlateResp;
import com.ljx.itbbs.server.user.domain.entity.UserPlateRelationEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-03-24  09:47
 */
public class ArticleAdapter {
    public static ArticleEntity buildArticleSave(ArticleSaveReq req, Long userId) {
        ArticleEntity articleEntity = new ArticleEntity();
        articleEntity.setUid(userId);
        articleEntity.setTitle(req.getTitle());
        articleEntity.setContent(req.getContent());
        articleEntity.setCover(req.getCover());
        articleEntity.setViewsNum(0);
        articleEntity.setLikeNum(0);
        articleEntity.setDislikeNum(0);
        articleEntity.setCollectNum(0);
        articleEntity.setCommentNum(0);
        articleEntity.setContainRes(ArticleResEnum.NOT_CONTAIN.getType());
        articleEntity.setTags(JSONUtil.toJsonStr(req.getTagList()));
        articleEntity.setPlateId(req.getPlateId());
        articleEntity.setCategoryId(req.getCategoryId());
        return articleEntity;
    }

    public static ArticleTagRelationEntity buildArticleTagRelationSave(Long id, Long articleId) {
        ArticleTagRelationEntity articleTagRelationEntity = new ArticleTagRelationEntity();
        articleTagRelationEntity.setArticleId(articleId);
        articleTagRelationEntity.setTagId(id);
        return articleTagRelationEntity;
    }

    public static LikeRecordEntity buildLikeRecordSave(String objKey, Long userId, Integer type) {
        LikeRecordEntity likeRecordEntity = new LikeRecordEntity();
        likeRecordEntity.setUserId(userId);
        likeRecordEntity.setLikeObj(objKey);
        likeRecordEntity.setType(type);
        return likeRecordEntity;
    }

    public static CollectRecordEntity buildCollectRecordSave(String objKey, Long userId, Integer type) {
        CollectRecordEntity collectRecordEntity = new CollectRecordEntity();
        collectRecordEntity.setUserId(userId);
        collectRecordEntity.setCollectObj(objKey);
        collectRecordEntity.setType(type);
        return collectRecordEntity;
    }

    public static ResourceEntity buildArticleResource(Long userId, ArticleResourceUploadReq req) {
        ResourceEntity resourceEntity = new ResourceEntity();
        resourceEntity.setUserId(userId);
        resourceEntity.setResUrl(req.getResUrl());
        resourceEntity.setFilename(req.getFilename());
        resourceEntity.setFilesize(req.getFilesize());
        resourceEntity.setFileType(req.getFileType());
        resourceEntity.setDownloadNum(0);

        return resourceEntity;
    }

    public static ResourceUserRelationEntity buildResourceRecordSave(Long userId, Long trueObjId) {
        ResourceUserRelationEntity resourceUserRelationEntity = new ResourceUserRelationEntity();
        resourceUserRelationEntity.setUserId(userId);
        resourceUserRelationEntity.setResourceId(trueObjId);
        return resourceUserRelationEntity;
    }

    public static ArticleEntity updateArticle(ArticleEntity article, ArticleUpdateReq req) {
        article.setTitle(req.getTitle());
        article.setContent(req.getContent());
        article.setCover(req.getCover());
        article.setPlateId(req.getPlateId());
        article.setCategoryId(req.getCategoryId());
        article.setTags(JSONUtil.toJsonStr(req.getTagList()));
        article.setContainRes(!req.getResourceList().isEmpty() ? ArticleResEnum.CONTAIN.getType() : ArticleResEnum.NOT_CONTAIN.getType());
        article.setStatus(article.getStatus());
        return article;
    }

    public static ArticleUpdateInfoResp buildArticleUpdateResp(ArticleEntity article, List<ResourceEntity> resourceList) {
        ArticleUpdateInfoResp articleUpdateInfoResp = new ArticleUpdateInfoResp();
        articleUpdateInfoResp.setId(article.getId());
        articleUpdateInfoResp.setTitle(article.getTitle());
        articleUpdateInfoResp.setContent(article.getContent());
        articleUpdateInfoResp.setCover(article.getCover());
        articleUpdateInfoResp.setPlateId(article.getPlateId());
        articleUpdateInfoResp.setCategoryId(article.getCategoryId());
        articleUpdateInfoResp.setTagList(JSONUtil.toList(article.getTags(), String.class));
        if (!resourceList.isEmpty()){
            ResourceEntity resourceEntity = resourceList.get(0);
            articleUpdateInfoResp.setResources(BeanUtil.copyToList(resourceList, ArticleUpdateInfoResp.ResourceInfo.class));
            articleUpdateInfoResp.setIsFree(resourceEntity.getIsFree());
            articleUpdateInfoResp.setIntegral(resourceEntity.getIntegral());
        } else {
            articleUpdateInfoResp.setResources(new ArrayList<>());
            articleUpdateInfoResp.setIsFree(null);
            articleUpdateInfoResp.setIntegral(null);
        }

        return articleUpdateInfoResp;
    }

    public static PlateEntity buildPlateSave(PlateAdminSaveReq req) {
        PlateEntity plateEntity = new PlateEntity();
        plateEntity.setName(req.getName());
        plateEntity.setSort(req.getSort());
        return plateEntity;
    }

    public static UserPlateRelationEntity buildUserPlateRelation(Long plateId, Long uid) {
        UserPlateRelationEntity userPlateRelationEntity = new UserPlateRelationEntity();
        userPlateRelationEntity.setPlateId(plateId);
        userPlateRelationEntity.setUid(uid);
        return userPlateRelationEntity;
    }

    public static PlateResp.UserInfo buildPlateUserInfo(Long uid, String nickname) {
//        return PlateResp.UserInfo.builder()
//                .id(uid)
//                .nickname(nickname)
//                .build();
        PlateResp plateResp = new PlateResp();
        PlateResp.UserInfo userInfo = plateResp.new UserInfo();
        userInfo.setId(uid);
        userInfo.setNickname(nickname);
        return userInfo;
    }
}
